package com.example.onlinemusic.config;

import com.example.onlinemusic.tools.Constant;
import com.example.onlinemusic.tools.ResponseBodyMessage;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Author 12629
 * @Date 2022/5/11 15:38
 * @Description：
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler) throws Exception {

        HttpSession httpSession = request.getSession(false);
        if(httpSession != null && httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) != null) {

            return true;
        }
        response.sendRedirect(request.getContextPath()+"/login.html"); //重定向跳转到登陆页面
        return false; //返回false，则不能继续下面的方法执行
    }
}
